import request from '@/utils/request'
import type { PageDto, PageReqDto, ProductType } from '../types'
import type { CompanyDto, ChannelDto, PeriodInfoDto, OpenAccountDto, CompanyInfoDto, UpdateCompanyInfoReqDto, PayWageInfoDto, CustomReqDto, CustomDto, ClaimDto, PeriodInfoReqDto, WesureInfoDto, GroupInfoDto, SaveTyxReqDto, EleSignInfoDto, ElesignTemplateReqDto, ElesignRecordReqDto, ElesignTemplateDto, RechargeReqDto, RechargeDto, AuditCountDto, CompanyProductInfoDto, CompanyDetailDto, PayRecordDto, CompanyProcessDto, CollectionAccountDto, CollectionAccountReqDto } from './types'
import { ChannelTypeCnEnum, type AccountTypeCnEnum, type ApprovalClientStatusCnEnum, type CollectionAccountStatusCnEnum, type CollectionAccountTypeCnEnum, type PayAccountFlagCnEnum, type PayModeCnEnum, type ProductTypeCnEnum, type ProductTypeEnum, type RechargeCheckStatusCnEnum } from '@/const/enum'


/**
 * 获取客户列表
 *
 * @param params 请求参数，包含 CustomReqDto 和 PageReqDto 的属性
 * @returns 返回一个包含 CustomDto 类型的分页结果 Promise
 */
export function getCustomList(params: PageReqDto & CustomReqDto): Promise<PageDto<CustomDto>> {
    // /crm-prod/saas/note/saasCompany/custom-list 老接口
    return request.get('/crm-prod/saas/note/saasCompany/queryCustomListNew', {
        params
    })
}

/**
 * 获取公司列表
 *
 * @param params 请求参数
 * @returns 返回 CompanyDto[] 类型的 Promise 对象
 */
export function getCompanyList(params: {
    companyName: string
    limit: number
    auditStatus?: number
    isDistinct?: number // 是否去重
}): Promise<CompanyDto[]> {
    return request.get('/crm-prod/saas/note/saasCompany/comb-co-info', {
        params
    })
}
/**
 * 获取渠道列表
 *
 * @returns 返回一个包含ChannelDto对象的Promise对象数组
 */
export function getChannelList(): Promise<ChannelDto[]> {
    return request.get(`/crm-prod/crm/channel/queryChannel?type=${ChannelTypeCnEnum.系统渠道}`, {
        params: {
            noCannel: true
        }
    })
}


// 导出账户详情
export function exportAccountDetails(params: {
    companyId?: number
    productType?: number
    type?: number
    payMode?: number
    startTime?: string
    endTime?: string
    startMonth?: string
    endMonth?: string
} & CollectionAccountReqDto): Promise<string> {
    return request.get('/crm-prod/saas/note/saasCompany/export-account-details', {
        params
    })
}

// 获取长期保信息
export function getPriodInfo(companyId: number): Promise<PeriodInfoDto> {
    return request.get(`/crm-prod/saas/note/saasCompany/query-period-info/${companyId}`)
}


/**
 * 获取已开通账户列表
 *
 * @returns 返回包含已开通账户信息的Promise对象，解析后得到OpenAccountDto类型的数组
 */
export function getOpenAccountList(): Promise<OpenAccountDto[]> {
    return request.get('/crm-prod/saas/note/saasCompany/query-open-account')
}


/**
 * 获取公司信息
 *
 * @param companyId 公司ID
 * @returns 返回公司信息对象 Promise
 */
export function getCompanyInfo(companyId: number): Promise<CompanyInfoDto> {
    return request.get(`/crm-prod/saas/note/saasCompany/query-company-info/${companyId}`)
}


/**
 * 更新公司信息
 *
 * @param data 更新公司信息所需的数据
 * @returns 返回更新结果的Promise对象，解析后为字符串类型的更新结果
 */
export function updateSaasCompany(data: UpdateCompanyInfoReqDto | PeriodInfoReqDto): Promise<string> {
    // /crm-prod/saas/note/saasCompany 老接口 put
    return request.post('/crm-prod/saas/note/saasCompany/updateBasicInfo', data)
}

/**
 * 导出公司信息
 *
 * @param params 请求参数
 * @param params.id 公司ID（可选）
 * @param params.ids 公司ID列表（可选）
 * @returns 返回一个Promise，解析为公司信息的字符串
 */
export function exportCompanyInfo(params: {
    id?: number,
    ids?: string
}): Promise<string> {
    return request.get('/crm-prod/saas/note/saasCompany/export-company-info', {
        params
    })
}


/**
 * 获取子弹发薪基本信息
 *
 * @param params 获取薪资信息的参数对象
 * @param params.companyId 公司ID
 * @returns 返回薪资信息的字符串
 */
export function getPayWageInfo(params: {
    companyId: number,
}): Promise<PayWageInfoDto> {
    return request.get('/crm-prod/saas/note/saasCompany/query-pay-info/', {
        params
    })
}

// 长期保 账户余额提现
export function periodWithdrawAccountBlance(data: {
    companyId: number,
    withdrawAmount: number
    remark: string
}): Promise<PayWageInfoDto> {
    return request.put('/crm-prod/saas/note/saasCompany/period-withdraw-account-balance', data)
}

// 理赔率详情
export function claimDetail(params: {
    companyId: number,
}): Promise<ClaimDto> {
    return request.get('/crm-prod/saas/note/saasCompany/co-claim-detail', {
        params
    })
}

// 导出理赔率
export function exportClaimDetail(params: {
    companyId: number,
}): Promise<string> {
    return request.get('/crm-prod/saas/note/saasCompany/export-co-claim-detail', {
        params
    })
}

// 长期保充值
export function rechargePeirodAccount(data: {
    companyId: number,
    payMode: number
    receiptId: number
    rechargeAmount: number
    voucherUrl: string
}): Promise<string> {
    return request.put('/crm-prod/saas/note/saasCompany/period-recharge-account-balance', data)
}

// 获取日日保信息
export function getWesureInfo(params: {
    companyId: number,
}): Promise<WesureInfoDto> {
    return request.get('/crm-prod/saas/note/saasCompany/query-wesure-info', {
        params
    })
}

// 日日保 账户余额提现
export function wesureWithdrawAccountBlance(data: {
    companyId: number,
    withdrawAmount: number
    remark: string
}): Promise<PayWageInfoDto> {
    return request.put('/crm-prod/saas/note/saasCompany/withdraw-account-balance', data)
}


// 日日保充值
export function rechargeWesureAccount(data: {
    companyId: number,
    payMode: number
    receiptId: number
    rechargeAmount: number
    voucherUrl: string
}): Promise<string> {
    return request.put('/crm-prod/saas/note/saasCompany/recharge-account-balance', data)
}


// 获取团意保信息
export function getGroupInfo(params: {
    companyId: number,
}): Promise<GroupInfoDto> {
    return request.get('/crm-prod/saas/note/saasCompany/query-tyx-info', {
        params
    })
}

// 保存团意保开通和编辑信息
export function saveTyInsureInfo(data: SaveTyxReqDto): Promise<GroupInfoDto> {
    return request.post('/crm-prod/saas/note/saasCompany/add-or-edit-tyInsur', data)
}

// 获取电子合同的基本信息
export function getElesignInfo(companyId: number): Promise<EleSignInfoDto> {
    return request.get(`/crm-prod/saas/note/saasCompany/query-ele-info/${companyId}`)
}

// 获取电子合同记录列表
export function getEleContractRecordList(params: PageReqDto & ElesignRecordReqDto): Promise<PageDto<EleSignInfoDto>> {
    return request.get('/crm-prod/saas/note/saasCompany/query-ele-contract-record', {
        params
    })
}

// 导出电子签合同记录
export function exportElesignContactRecord(params: ElesignRecordReqDto): Promise<string> {
    return request.get('/crm-prod/saas/note/saasCompany/export-ele-sign-contract-record', {
        params
    })
}

// 获取合同模板列表
export function getEleContractTemplateList(params: PageReqDto & ElesignTemplateReqDto): Promise<PageDto<ElesignTemplateDto>> {
    return request.get('/crm-prod/saas/note/saasCompany/query-contract-template', {
        params
    })
}

// 导出合同模板
export function exportContractTemplate(params: {
    searchName?: string
    userName?: string
    companyId?: number
    companyName: string
}): Promise<string> {
    return request.get('/crm-prod/saas/note/saasCompany/export-contract-template', {
        params
    })
}



// 获取充值记录列表
export function getRechargeRecordList(params: PageReqDto & RechargeReqDto): Promise<PageDto<RechargeDto>> {
    return request.get('/crm-prod/saas/note/saasCompany/recharge-record-list', {
        params
    })
}

// 导出充值审批记录
export function exportRecharge(params: {
    companyName: string
    productType: ProductType
    status: RechargeCheckStatusCnEnum | ''
}): Promise<string> {
    return request.get('/crm-prod/saas/note/saasCompany/export-recharge', {
        params
    })
}

/**
 * 获取审批的数量
 * @returns 
 */
export function getAuditCount(): Promise<AuditCountDto> {
    return request.get('/crm-prod/saas/note/saasCompany/need-audit-count')
}


/**
 * 获取客户产品信息
 * @param params 
 * @returns 
 */
export function getCompanyProductInfo(params: {
    companyId: number
}): Promise<CompanyProductInfoDto> {
    return request.get('/crm-prod/saas/note/saasCompany/queryCompanyProductInfo', {
        params
    })
}


/**
 * 获取客户基本信息
 * @param params 
 * @returns 
 */
export function getCompanyDetail(params: {
    companyId: number
}): Promise<CompanyDetailDto> {
    return request.get('/crm-prod/saas/note/saasCompany/queryCompanyDetail', {
        params
    })
}
/**
 * 获取公司发薪记录
 * @param params 
 * @returns 
 */
export function getPayRecordList(params: PageReqDto & {
    companyId: number
    projectName?: string
    beginPayTime?: string
    endPayTime?: string
    payAccountFlag?: PayAccountFlagCnEnum
}): Promise<PageDto<PayRecordDto>> {
    return request.get('/crm-prod/saas/note/saasCompany/queryPayRecord', {
        params
    })
}

/**
 * 删除公司
 * @param data 
 * @returns 
 */
export function deleteCompany(params: {
    companyId: number
}): Promise<CompanyDetailDto> {
    return request.delete('/crm-prod/saas/note/saasCompany/deleteCompany', {
        params
    })
}

/**
 * 审批管理客户审批列表
 * @param params 
 * @returns 
 */
export function getCompanyProcessList(params: PageReqDto & Partial<{
    companyName: string
    applyUserName: string
    queryStartDate: string
    queryEndDate: string
}>): Promise<PageDto<CompanyProcessDto>> {
    return request.get('/crm-prod/saas/company/processRecord/queryCompanyProcessRecord', {
        params
    })
}


/**
 * @description 审核公司
 * @param data
 * @returns
 */
export function processRecord (data: {
    id: number
    channelId?: number
    receiptId?: number
    status: ApprovalClientStatusCnEnum
}): Promise<void> {
    return request.post('/crm-prod/saas/company/processRecord/process', data)
}


/**
 * 收款账户明细列表
 * @param params 
 * @returns 
 */
export function getCollectionAccountList (params: PageReqDto & CollectionAccountReqDto): Promise<PageDto<CollectionAccountDto>> {
    return request.get('/crm-prod/saas/note/saasCompany/query-account-details', {
        params
    })
}

/**
 * 编辑充值信息
 * @param data 
 * @returns 
 */
export function editRechargeAccountInfo (data: {
    id: number
    payMode: PayModeCnEnum
    productType: ProductTypeCnEnum
    receiptId: number
    remark: string
}): Promise<void> {
    return request.post('/crm-prod/saas/note/saasCompany/edit-recharge-account-info', data)
}

